package com.wopinla.service.impl;

import com.wopinla.dao.ExampleDao;
import com.wopinla.dao.goodsDao;
import com.wopinla.pojo.*;
import com.wopinla.dao.ShoppingcarDao;
import com.wopinla.pojo.shoppingcar;
import com.wopinla.service.ShoppingcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service   //添加注解
public class ShoppingcarServiceImpl implements ShoppingcarService{

    //注入dao层（调用dao）

    @Autowired
    private ShoppingcarDao shoppingcarDao;

    @Autowired
    private goodsDao goodsdao;

    public int addshoppingcar(Map<String, Object> map)
    {
        return  shoppingcarDao.addshoppingcar(map);
    }

    @Override
    public int deleteshoppingcar(Map<String, Object> map)
    {
        return  shoppingcarDao.deleteshoppingcar(map);
    }

    @Override
    public List<CartResult> findshoppingcar(int uid) {
        List<shoppingcar> shoppingcars = shoppingcarDao.findshoppingcar(uid);
        System.out.println(shoppingcars.size());
        List<CartResult> goodsList = new ArrayList<CartResult>();
        for(shoppingcar cart : shoppingcars){
            goods good = goodsdao.goodsdetailsById(cart.getGid());
            CartResult cartResult = new CartResult();
            cartResult.setSid(good.getSid());
            cartResult.setPrice(good.getPrice());
            cartResult.setAvatarUrl(good.getAvatarUrl());
            cartResult.setName(good.getName());
            cartResult.setNum(cart.getNum());
            cartResult.setAllPrice(cart.getNum() * good.getPrice());
            cartResult.setType(good.getType());
            cartResult.setEid(good.getEid());
            goodsList.add(cartResult);
        }
        return goodsList;
    }

}
